/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.solr.collection.impl;

import java.io.File;
import java.util.List;

import com.pingan.haofang.searchcloud.api.StorageCluster;
import com.pingan.haofang.searchcloud.api.constants.EngineType;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.cloud.ZkConfigManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.pingan.haofang.searchcloud.api.schema.IndexSchema;
import com.pingan.haofang.searchcloud.solr.api.client.SolrClientFactory;
import com.pingan.haofang.searchcloud.api.CollectionConfigManager;
import com.pingan.haofang.searchcloud.solr.schema.SolrCloudXmlGenerater;

/**
 * @author huhailiang937
 * @date 2017年5月4日下午4:34:13
 * @since 1.0.0
 */
@Component
public class SolrCollectionConfigManager implements CollectionConfigManager {

    protected static Logger LOG = LoggerFactory.getLogger(SolrCollectionConfigManager.class);

    @Autowired
    protected SolrClientFactory clientFactory;

    /**
     * 新增zk中的 /configs 配置文件
     *
     * @param zkHost         host
     * @param schema         元数据
     * @param configNameInZk zk中configs的名称
     * @return
     */
    @Override
    public boolean create(String zkHost, IndexSchema schema, String configNameInZk) {
        Assert.notNull(configNameInZk, "configNameInZk can not be null");
        Assert.notNull(schema, "schema can not be null");
        String indexName = schema.getIndexName();
        Assert.notNull(indexName, "schema.indexName can not be null");
        Assert.notNull(schema.getUniqueKey(), "schema.uniqueKey can not be null");

        SolrCloudXmlGenerater solrCloudXmlGenerater = new SolrCloudXmlGenerater();

        try {
            solrCloudXmlGenerater.generatorIndexSchemaXml(schema);
        } catch (Exception e) {
            LOG.error("solrCouldXmlGenerater generatorIndexSchemaXml has error", e);
            return false;
        }

        try {
            solrCloudXmlGenerater.generatorSolrconfigXml(schema);
        } catch (Exception e) {
            LOG.error("solrCouldXmlGenerater generatorSolrconfigXml has error", e);
            return false;
        }

        try {
            solrCloudXmlGenerater.generatorOthersXml(schema);
        } catch (Exception e) {
            LOG.error("solrCouldXmlGenerater generatorOthersXml has error", e);
            return false;
        }

        // 保持在本地的目录
        String schemaDir = solrCloudXmlGenerater.getGeneratorDir(indexName);

        try {
            ZkConfigManager zkConfigManager = this.getZkConfigManager(zkHost);
            zkConfigManager.uploadConfigDir(new File(schemaDir).toPath(), configNameInZk);
        } catch (Exception e) {
            LOG.error(String.format("uploadConfigDir solrconfig path[%s] has error", schemaDir), e);
            return false;
        }

        return true;
    }

    /**
     * @param zkHost
     * @param configNameInZk zk中configs的名称
     * @return
     */
    @Override
    public boolean delete(String zkHost, String configNameInZk) {
        try {
            ZkConfigManager zkConfigManager = this.getZkConfigManager(zkHost);
            zkConfigManager.deleteConfigDir(configNameInZk);
            LOG.info("solr configDir {} deleted", configNameInZk);
            return true;
        } catch (Exception e) {
            LOG.error(String.format("zkConfigManager.deleteConfigDir(%s)", configNameInZk), e);
            throw new RuntimeException(String.format("zkConfigManager.deleteConfigDir(%s)", configNameInZk), e);
        }
    }

    /**
     * 获取所有ConfigNames
     *
     * @param zkHost
     * @return
     */
    @Override
    public List<String> getAllConfigNames(String zkHost) {
        try {
            ZkConfigManager zkConfigManager = this.getZkConfigManager(zkHost);
            return zkConfigManager.listConfigs();
        } catch (Exception e) {
            LOG.error(String.format("zkConfigManager.listConfigDir(%s)", zkHost), e);
            throw new RuntimeException(String.format("zkConfigManager.listConfigDir(%s)", zkHost), e);
        }
    }


    private ZkConfigManager getZkConfigManager(String zkHost) throws Exception {
        CloudSolrClient solrClient = clientFactory.getByHost(zkHost);
        SolrZkClient zkClient = solrClient.getZkStateReader().getZkClient();
        return new ZkConfigManager(zkClient);

    }

    @Override
    public EngineType getEngineType() {
        return EngineType.SOLR;
    }
}
